package ActiveMQ.testHelloWorld;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * Created by brossdong on 2017/12/15.
 */
public class TestReceiver {

    public static void main(String[] args) throws Exception {
        //1.建立ConnectionFactory工厂对象,需要填入用户名和密码,默认端口"tcp://localhost:61616"
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
                ActiveMQConnectionFactory.DEFAULT_USER,
                ActiveMQConnectionFactory.DEFAULT_PASSWORD,
                "tcp://localhost:61616");
        //2.通过ConnectionFactory工厂对象创建一个Connection对象,并用start开启连接
        Connection connection = connectionFactory.createConnection();
        connection.start();
        //3.通过ConnectionFactory工厂对象创建session会话,用于接收信息,参数配置1为是否开启事物,参数2为签收模式 , 一般设置为自动签收
        Session session = connection.createSession(Boolean.FALSE,Session.CLIENT_ACKNOWLEDGE);
        //4.通过session创建destination,指的是一个客户端用来指定生产消息目标和消费信息来源的对象
        Queue destination = session.createQueue("queue1");
        //5.通过session创建消息的发送和接收对象(生产者和消费者)
        MessageConsumer consumer = session.createConsumer(destination);

        while (true) {
            TextMessage message = (TextMessage) consumer.receive();
            //手动去签收消息
            message.acknowledge();
            if (message == null) break;
            System.out.println("收到内容" + message.getText());
        }

        if(connection != null) {
            connection.close();
        }

    }
}
